class Solution {
    public int shipWithinDays(int[] weights, int days) {
        int left=0, right=0;
        for(int weight : weights){
            right += weight;
            left = Math.max(left, weight);
        }
        while(left < right){
            int mid = (left + right)>>1;
            if(check(weights, days, mid)){
                right = mid;
            }else{
                left = mid+1;
            }
        }
        return right;
    }

    private boolean check(int[] weights, int days, int size){
        int day = 1;
        int content = 0;
        for(int weight : weights){
            if(content + weight <= size){
                content += weight;
            }else{
                content = weight;
                day++;
            }
        }
        return day <= days;
    }
}

/**
    可以看做数组分成 连续M段，求每段和最大值的最小值
 */